package com.milhouz.wormdroid.gl.model.anim;
import java.util.ArrayList;

import android.os.SystemClock;

public class Animation {

	public String mAnimationName;
	
	private ArrayList<TexRect> mAnimation;
	private ArrayList<Integer> mDelay;
	private long mTimer;
	private int mIndex;
	private boolean loop;
	private boolean looping;

	public Animation(String animationName) {
		this.mAnimationName = animationName;
		this.mIndex = 0;
		this.mTimer = SystemClock.elapsedRealtime();
		this.loop = false;
		this.looping = false;
		this.mAnimation = new ArrayList<TexRect>();
		this.mDelay = new ArrayList<Integer>();
  }

	public void add(int x, int y, int w, int h, int delay) {
	    mAnimation.add(new TexRect(x, y, w, h));
	    this.mDelay.add(delay);
	}

	public TexRect next() {
    if (mTimer + mDelay.get(mIndex) < SystemClock.elapsedRealtime()) {
        if (loop) {
            if (mIndex == mAnimation.size() - 1) looping = true;
            else if (mIndex == 0) looping = false;
            if (looping)
                mIndex--;
            else
                mIndex++;
        } else {
            mIndex++;
            if (mIndex == mAnimation.size())
                mIndex = 0;
        }
        mTimer = SystemClock.elapsedRealtime();
    }
    return mAnimation.get(mIndex);
	}

	public void loop(boolean loop) {
	    this.loop = loop;
	}

	public TexRect getFirstTexRect() {
		if (mAnimation.size() > 0)
			return mAnimation.get(0);
		else
			return null;
	}
}
